package aiku.numericalcontroloa.project.system.controller;

import aiku.numericalcontroloa.common.base.BaseController;
import aiku.numericalcontroloa.common.base.Result;
import aiku.numericalcontroloa.project.system.pojo.entity.SysRole;
import aiku.numericalcontroloa.project.system.pojo.query.SysRoleParams;
import aiku.numericalcontroloa.project.system.service.SysRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/sysRole")
public class SysRoleController extends BaseController {
    @Resource
    SysRoleService sysRoleService;


    /**
     * 获取角色对应的权限
     *
     * @param params
     * @return
     */
    @GetMapping("/rolePerms")
    public Result rolePerms(SysRoleParams params) {
        return success(sysRoleService.listRolePerms(params.getId()));
    }

    /**
     * 添加和修改角色对应的权限
     *
     * @param params
     * @return
     */
    @PostMapping("/handlePerms")
    public Result handlePerms(@RequestBody SysRoleParams params) {
        if (sysRoleService.handlePerms(params) == 0) {
            return error("保存角色权限信息失败");
        }
        return success(sysRoleService.handlePerms(params));
    }

    /**
     * 新增角色
     *
     * @param role
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody SysRole role) {
        return success(sysRoleService.insert(role));
    }

    /**
     * 获取角色分页数据
     *
     * @return
     */
    @GetMapping("/pageList")
    public Result pageList() {
        return success(sysRoleService.pageList());
    }

    /**
     * 更新角色信息
     *
     * @param role
     * @return
     */
    @PostMapping("/update")
    public Result updateRole(@RequestBody SysRole role) {
        return success(sysRoleService.updateByPrimaryKey(role));
    }

    /**
     * 删除角色
     *
     * @param query
     * @return
     */
    @PostMapping("/delete")
    public Result deleteRole(@RequestBody SysRoleParams query) {
        int result = sysRoleService.deleteRole(query);
        if (result == -1) {
            return error("该角色存在用户，删除失败");
        } else if (result == -2) {
            return error("该角色已分配菜单权限，删除失败");
        }
        return success("删除角色成功");
    }


    @GetMapping("/list")
    public Result list() {
        return success(sysRoleService.list());
    }

    @GetMapping("/listByUserId")
    public Result listByUserId(SysRoleParams query) {
        return success(sysRoleService.listByUserId(query));
    }


    @GetMapping("/ListByEnabled")
    public Result ListByEnabled() {
        return success(sysRoleService.ListByEnabled());
    }

    @PostMapping("/status")
    public Result updateRoleStatus(@RequestBody SysRole role) {
        return success(sysRoleService.updateRoleStatus(role));

    }
}
